(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:pager
     * @description
     * # pager 
     */
    angular.module('webApp')
        .directive('weekSelect', function() {
            return {
                restrict: "E",
                templateUrl: "views/directives.weekselect.html",
                require: 'ngModel',
                scope: {
                    ngModel: '=',
                    callback: "&",
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper) {
                    $scope.init = function() {
                        $scope.ngModel = $scope.ngModel ? $scope.ngModel : [];
                        $scope.weeks = [{
                            id: "1",
                            name: "周一",
                            en: "Monday",
                        }, {
                            id: "2",
                            name: "周二",
                            en: "Tuesday",
                        }, {
                            id: "3",
                            name: "周三",
                            en: 'Wednesday',
                        }, {
                            id: "4",
                            name: "周四",
                            en: 'Thursday',
                        }, {
                            id: "5",
                            name: "周五",
                            en: 'Friday',
                        }, {
                            id: "6",
                            name: "周六",
                            en: 'Saturday',
                        }, {
                            id: "7",
                            name: "周日",
                            en: 'Sunday',
                        }];
                    }
                    $scope.check = function(id, ck) {
                        console.error("id,", id);
                        if ($scope.callback) {
                            $scope.callback({ da: $scope.ngModel });
                        }
                    }

                    $scope.init();
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        })
        .directive('dateSelect', function() {
            return {
                restrict: "E",
                templateUrl: "views/directives.dateselect.html",
                require: 'ngModel',
                scope: {
                    ngModel: '=',
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', 'moment', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, moment) {
                    $scope.start_created = moment().format('YYYYMMDD');
                    $scope.callback = function(date) {
                        $scope.ngModel = $scope.ngModel ? $scope.ngModel : [];
                        if (date) {
                            var i;
                            _.map($scope.ngModel, function(d) {
                                if (d == date) {
                                    i = 1;
                                }
                            });
                            if (!i) {
                                $scope.ngModel.push(date);
                            }
                        }
                    }
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        })
        .directive('editTemplate', function() {
            return {
                restrict: "E",
                template: "<span><a href='javascript:void(0);' class='sui-btn btn-success'  ng-click='showPop();'>{{'编辑' | translate}}</a></span>",
                scope: {
                    // setback: "&",
                    tmpid: "=",
                    type: "@",
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', '$interval', 'moment', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, $interval, moment) {
                    $scope.showPop = function() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '编辑模板',
                            body: '<span><resnews-cycle-list tmpid="tmpid" type="type"></resnews-cycle-list></span>',
                            // okBtn: '{{"ok" | translate
                            closeBtn: true,
                            transition: true,
                            hasfoot: false,
                            width: 800,
                            height: 500,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }


                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .directive('resTemplateList', function() {
            return {
                restrict: "E",
                template: ' <span ng-show="stype">{{title}}</span><span ng-show="!stype"><select ng-model="SelModer" ng-options="data.template_id as data.title group by data.type_name for data in period_stls" ng-change="change(SelModer)" ></select></span>',
                require: 'ngModel',
                scope: {
                    // setback: "&",
                    ngModel: '=',
                    stype: "=",
                    dft: "=",
                    apitype: "@",
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', '$interval', 'moment', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, $interval, moment) {
                    $scope.init = function(schemaid) {
                        var query = { query: { "store_id": $rootScope.store_id } };
                        if ($scope.stype) {
                            if ($scope.apitype) {
                                query.query.res_type_id = $scope.ngModel;
                            } else {
                                query.query.template_id = $scope.ngModel;
                            }
                        }

                        var api = $scope.apitype ? $scope.apitype : "templateGet";
                        dataService[api](query).then(function(re1) {
                            if (re1.templates && re1.templates.length) {
                                $scope.period_stls = re1.templates;
                                _.map($scope.period_stls, function(da, ll) {
                                    if (da.type_id == 1) {
                                        da.type_name = "按周设置";
                                    }
                                    if (da.type_id == 2) {
                                        da.type_name = "按天设置";
                                    }
                                });
                                if ($scope.stype) {
                                    $scope.title = $scope.period_stls[0].title;
                                }
                                if ($scope.dft) {
                                    $scope.SelModer = $scope.ngModel = $scope.period_stls[0].template_id;
                                }
                                // console.error("$scope.SelModer ", $scope.SelModer, $scope.period_stls);
                            }
                            if (re1.res_types) {
                                if ($scope.stype) {
                                    $scope.title = re1.res_types[0].name;
                                }
                            }
                        });
                    }
                    $scope.change = function(newValueid) {
                        if (newValueid) {
                            $scope.ngModel = newValueid;
                        }
                    }

                    $scope.init();
                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .directive('resnewsCycleList', function() {
            return {
                restrict: "E",
                templateUrl: "views/directives.resnewscyclelist.html",
                scope: {
                    // setback: "&",
                    tmpid: "=",
                    callback: "&"
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', '$interval', 'moment', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, $interval, moment) {

                    $scope.init = function(schemaid) {
                        var query = { query: { "template_id": $scope.tmpid } };

                        dataService.templateGet(query).then(function(re1) {
                            if (re1.templates.length) {
                                var tdata = JSON.parse(re1.templates[0].periods);
                                $scope.period_stls = clone(re1.templates[0]);
                                $scope.period_stls.periods = tdata.periods;
                                $scope.period_stls.exclude_by_week = $scope.period_stls.exclude_by_week ? $scope.period_stls.exclude_by_week.split(",") : [];
                                $scope.period_stls.exclude_by_date = $scope.period_stls.exclude_by_date ? $scope.period_stls.exclude_by_date.split(",") : [];
                                $scope.weekciclk();
                                console.error($scope.period_stls, tdata);
                            } else {

                            }
                        });
                    }
                    //根据用户选择的 周几避开进行展示
                    $scope.weekciclk = function(da) {
                        //console.error("period_stls.exclude_by_week", $scope.period_stls.exclude_by_week);
                        $timeout(function() {
                            _.map($scope.period_stls.periods, function(da, kk) {
                                da.show = 0;
                                if ($scope.period_stls.exclude_by_week.length && $scope.period_stls.type_id == 1) {
                                    if ($scope.period_stls.exclude_by_week.indexOf(String(da.period_no)) >= 0) {
                                        da.show = 1;
                                    }
                                }
                            });
                            //console.error("period_stls.exclude_by_week", $scope.period_stls.periods);
                        });
                    }

                    $scope.Testing = function(end, start) {
                        if (end == start) {
                            helper.myAlert("开始时间不能和结束时间相同!");
                            end = '0';
                        }
                    }
                    //t添加
                    $scope.addtimeslots = function(data) {
                        var a = {
                            end: "0",
                            max_count: 3,
                            start: "0",
                        };

                        data.push(a);
                    }

                    $scope.saveData = function() {
                        $timeout(function() {
                            var data = clone($scope.period_stls);
                            data.exclude_by_week = data.exclude_by_week.length > 0 ? data.exclude_by_week.toString() : data.exclude_by_week;
                            data.exclude_by_date = data.exclude_by_date.length > 0 ? data.exclude_by_date.toString() : data.exclude_by_date;
                            data.periods = JSON.stringify({ periods: data.periods });
                            return dataService.templateUpdate(data).then(function(re1) {
                                helper.myAlert("success", null, null, function() {
                                    rmAlert();
                                });
                            });
                        });
                    }
                    $scope.init();

                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.editperiods.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('editPeriods', function() {
            return {
                restrict: "E",
                template: "<span><a href='javascript:void(0);' class='sui-btn btn-success'  ng-click='init();'>{{title | translate}}</a></span>",
                scope: {
                    // setback: "&",
                    schemaid: "=",
                    title: "@",
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', '$interval', 'moment', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, $interval, moment) {
                    $scope.init = function() {
                        $scope.getapi().then($scope.showPop());
                    }

                    $scope.showPop = function() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '编辑模板',
                            body: $templateCache.get("views/directives.editperiods.html"),
                            // okBtn: '{{"ok" | translate
                            closeBtn: true,
                            transition: true,
                            hasfoot: false,
                            width: 800,
                            height: 500,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }

                    $scope.addtimeslots = function(data) {
                        var a = {
                            end: "0",
                            max_count: 3,
                            start: "0",
                        };

                        data.push(a);
                    }

                    //删除
                    $scope.deletetime = function(periods_id, res_item_id, timeslots_id) {
                        helper.myConfirm("确认删除吗？", function() {
                            var data = { periods_id: periods_id, res_item_id: res_item_id };
                            if (timeslots_id) {
                                data.timeslots = JSON.stringify({ timeslots: [{ timeslots_id: timeslots_id }] });
                            }

                            dataService.periodsDelete(data).then(function(da) {
                                $scope.getapi();
                            });
                        })
                    }
                    $scope.saveData = function() {
                        async.map($scope.periods, function(data, callback) {
                            if (data) {
                                var da = clone(data);
                                da.timeslots = JSON.stringify({ timeslots: da.timeslots });
                                dataService.periodsUpdate(da).then(function(r) {
                                    callback(null);
                                })
                            } else {
                                callback(null);
                            }
                        }, function(e, r) {
                            helper.myAlert("success", null, null, function() {
                                rmAlert();
                            });
                        });
                    }
                    $scope.getapi = function(re1) {
                        return dataService.periodsGet({ query: { res_item_id: $scope.schemaid } }).then(function(re1) {
                            console.error("re1", re1);
                            if (re1.periods) {
                                $scope.periods = re1.periods;
                            }
                        })

                    }

                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .directive('periodstlList', function() {
            return {
                restrict: "E",
                templateUrl: "views/directives.periodstl.html",
                scope: {
                    // setback: "&",
                    schemaid: "=",
                    callback: "&"
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', '$interval', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, $interval) {

                    var num = 0;
                    $scope.init = function(schemaid) {
                        dataService.periodstlGet({ query: { schema_id: schemaid } }).then(function(re1) {
                            if (re1.period_stls.length > 0) {
                                $scope.periocd = {};
                                if (re1.period_stls) {
                                    $scope.periocd = re1.period_stls;
                                }
                                if (schemaid) {
                                    $scope.uptype = 1;
                                }
                                $scope.periocd = _.sortBy($scope.periocd, 'period_stl_id');
                            } else {
                                num = num + 1
                                if (num <= 4) {
                                    $scope.init();
                                    if (!schemaid) {
                                        $scope.kaiqi();
                                    }
                                }

                            }
                        })
                    }

                    $scope.kaiqi = function() {
                        dataService.reserveoptAdd().then(function(r) {

                        })
                    }

                    $scope.saveData = function() {
                        async.map($scope.periocd, function(data, callback) {
                            if (data) {
                                var iad = { "period_stl_id": data.period_stl_id, status: data.status }
                                dataService.periodstlUpdate(iad).then(function(r) {
                                    callback(null);
                                })
                            } else {
                                callback(null);
                            }
                        }, function(e, r) {
                            helper.myAlert("success", null, null, function() {
                                if ($scope.schemaid && !$scope.uptype) {
                                    $scope.schemAdd();
                                }
                                if ($scope.callback) {
                                    $scope.callback();
                                } else {
                                    rmAlert();
                                }
                            });
                        });
                    }
                    $scope.init($scope.schemaid ? $scope.schemaid : 0);
                    $scope.schemAdd = function() {
                        dataService.periodstlAdd({ schema_id: $scope.schemaid }).then(function(r) {})
                    }
                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .directive('timeslotstlList', function() {
            return {
                restrict: "E",
                templateUrl: "views/directives.timeslotstl.html",
                scope: {
                    schemaid: "=",
                    periodtstlid: "=",
                    callback: "&"
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper) {

                    $scope.delData = function(k, id) {
                        _.map($scope.deskData, function(da, ke) {
                            if (ke == k) {
                                if (da.timeslot_stl_id) {
                                    var query = { "timeslot_stl_id": id };
                                    dataService.timeslotstlDelete(query).then(function(list) {
                                        $scope.deskData.splice(k, 1);
                                        $scope.init($scope.schemaid ? $scope.schemaid : 0, $scope.periodtstlid ? $scope.periodtstlid : 0);
                                    });
                                }
                            }
                        });

                    }
                    var num = 0;

                    $scope.init = function(schemaid, periodtstlid) {
                        var query = { schema_id: schemaid, period_stl_id: periodtstlid };
                        dataService.timeslotstlGet({ query: query }).then(function(re1) {
                            if (re1.timeslot_stls.length > 0) {
                                $scope.deskData = {};
                                if (re1.timeslot_stls) {
                                    $scope.time = re1.timeslot_stls;
                                }
                                if (schemaid) {
                                    $scope.timuptype = 1;
                                }
                                $scope.deskData = _.sortBy($scope.time, 'timeslot_no');
                            } else {
                                num = num + 1
                                if (num <= 4) {
                                    $scope.init();
                                }
                            }
                        })
                    }

                    $scope.Testing = function(value, type, key) {
                        _.map($scope.deskData, function(da, ke) {
                            if (key == ke) {
                                if (da.start == da.end) {
                                    helper.myAlert("开始时间不能和结束时间相同!");
                                    da.end = '0';
                                }
                            }
                        });
                    }

                    $scope.addDeskData = function() {
                        $scope.init = { "start": "0", "end": "0", "max_count": 0, "status": 1 };
                        $scope.deskData.push($scope.init);
                    }

                    $scope.saveData = function() {

                        var istype;
                        _.map($scope.deskData, function(da, k) {
                            if (da.start == '0' || da.end == '0') {
                                helper.myAlert("时间填写不能为空!");
                                istype = 1;
                            }
                            if (da.start == da.end) {
                                helper.myAlert("开始时间不能和结束时间相同!" + da.start + "和" + da.end);
                                istype = 1;
                            }
                        });
                        if (istype) {
                            return false;
                        }
                        //保存数据
                        async.mapValues($scope.deskData, function(data, key, callback) {
                            data.period_stl_id = $scope.periodtstlid ? $scope.periodtstlid : 0;
                            if (!data.timeslot_stl_id) {
                                delete data.$$hashKey;

                                if ($scope.timuptype) {
                                    data.schema_id = $scope.schemaid ? $scope.schemaid : 0;
                                }

                                data.timeslot_no = key + 1;

                                dataService.timeslotstlAdd(data).then(function(r) {
                                    callback(null);
                                })
                            } else {
                                dataService.timeslotstlUpdate(data).then(function(r) {
                                    callback(null);
                                })
                            }
                        }, function(e, r) {
                            helper.myAlert("success", null, null, function() {
                                if ($scope.schemaid && !$scope.timuptype) {
                                    $scope.timAdd();
                                }
                                if ($scope.callback) {
                                    $scope.callback();
                                } else {
                                    rmAlert();
                                }
                            });
                        });
                    }


                    $scope.timAdd = function() {
                        dataService.groupTimeslotstlAdd({ schema_id: $scope.schemaid }).then(function(r) {
                            // /rest/v1/group/timeslotstl/add
                        })
                    }


                    $scope.init($scope.schemaid ? $scope.schemaid : 0, $scope.periodtstlid ? $scope.periodtstlid : 0);
                }],
                link: function(scope, element, attrs, ctrl) {

                }
            }
        })
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.basic.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('basicSettings', function() {
            return {
                restrict: "E",
                template: "<span><a href='javascript:void(0);' class='sui-btn btn-success'  ng-click='showPop();'>{{'周期设置' | translate}}</a></span>",
                scope: {
                    name: "@",
                    schemaid: "=",
                    // stype: "=",
                    periodtstlid: "=",
                    // callback: "&"
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout) {
                    $scope.tabType = "periodst";
                    $scope.isname = $scope.name + "预约周期设置";
                    $scope.showPop = function() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '{{isname | translate}}',
                            body: $templateCache.get("views/directives.basic.html"),
                            // okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            hasfoot: false,
                            width: 'large',
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }

                    $scope.stltodat = function() {
                        var query = { "schema_id": $scope.schemaid };
                        dataService.stltodataAdd(query).then(function(re1) {
                            rmAlert();
                        });
                    }
                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        // period-data
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.private.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('periodData', function() {
            return {
                restrict: "E",
                template: "<span><a href='javascript:void(0);' class='sui-btn btn-warning'  ng-click='showinit();'>{{'实例详情' | translate}}</a></span>",
                scope: {
                    name: "@",
                    schemaid: "=",
                    // stype: "=",
                    periodtstlid: "="
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout) {
                    $scope.tabType = "periodst";
                    $scope.isname = $scope.name + "项目定制数据";
                    $scope.showinit = function() {
                        $scope.getapi();
                        // $scope.showPop();
                    }

                    $scope.showPop = function() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '{{isname | translate}}',
                            body: "",
                            // $templateCache.get("views/directives.private.html")
                            // okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            hasfoot: false,
                            width: 'small',
                            // height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }

                    $scope.getapi = function(re1) {
                        return dataService.periodsGet({ query: { res_item_id: $scope.schemaid } }).then(function(re1) {
                            console.error("re1", re1);
                        })

                    }

                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.private.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('privatesData', function() {
            return {
                restrict: "E",
                template: "<span><a href='javascript:void(0);' class='sui-btn btn-warning'  ng-click='showinit();'>{{'实例详情' | translate}}</a></span>",
                scope: {
                    name: "@",
                    schemaid: "=",
                    // stype: "=",
                    periodtstlid: "="
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout','helper', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout,helper) {
                    $scope.tabType = "periodst";
                    $scope.isname = $scope.name + "项目定制数据";
                    $scope.showinit = function() {
                        $scope.getrqi().then(function() {
                            $timeout(function() {
                                if ($scope.disary && !$scope.type) {
                                    $scope.showPop();
                                }
                            });
                        });
                    }

                    $scope.showPop = function() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '{{isname | translate}}',
                            body: '<newdatetimepickerlist ng-model="schemaid" disary="{{disary}}"></newdatetimepickerlist>',
                            // okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            hasfoot: false,
                            width: 'small',
                            // height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }

                    $scope.getrqi = function(re1) {
                        return dataService.periodsGet({ query: { res_item_id: $scope.schemaid } }).then(function(re1) {
                            if (re1.periods) {
                                $scope.disary = _.sortBy(re1.periods, 'source_at');
                                var source = $scope.disary[0].source_at;
                                $scope.num = $scope.disary.length;
                                // $scope.minDate = moment().format('YYYY-MM-DD');
                                if (moment(moment().format('YYYY-MM-DD')).isBefore(source)) {
                                    $scope.minDate = source;
                                } else {
                                    $scope.minDate = moment().format('YYYY-MM-DD');
                                }
                                $scope.maxDate = $scope.disary[$scope.num - 1].source_at;
                                $scope.getDate();
                            }
                        })
                    }

                    $scope.getDate = function() {
                        var startTime = new Date($scope.minDate);
                        var endTime = new Date( $scope.maxDate);
                        if (endTime.getTime() < startTime.getTime()) {
                            $scope.type =1;
                            helper.myAlert("数据过期，请重新生成！");
                        }
                    }
                    // $scope.stltodat = function() {
                    //     var query = { "schema_id": $scope.schemaid };
                    //     dataService.stltodataAdd(query).then(function(re1) {
                    //         rmAlert();
                    //     });
                    // }
                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.private.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('privateData', function() {
            return {
                restrict: "E",
                template: "<span><a href='javascript:void(0);' class='sui-btn btn-warning'  ng-click='showinit();'>{{'实例详情' | translate}}</a></span>",
                scope: {
                    name: "@",
                    schemaid: "=",
                    // stype: "=",
                    periodtstlid: "="
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout) {
                    $scope.tabType = "periodst";
                    $scope.isname = $scope.name + "项目定制数据";
                    $scope.showinit = function() {
                        $scope.getrqi().then(function() {
                            $scope.showPop();
                        });
                    }

                    $scope.showPop = function() {
                        $.alert({
                            backdrop: 'static',
                            keyboard: true,
                            title: '{{isname | translate}}',
                            body: $templateCache.get("views/directives.private.html"),
                            // okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            hasfoot: false,
                            width: 'small',
                            // height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {

                            },
                        });
                    }

                    $scope.getrqi = function(re1) {
                        return dataService.periodGet({ query: { schema_id: $scope.schemaid, status: 0 } }).then(function(re1) {
                            if (re1.period_stls) {
                                $scope.disary = [];
                                _.map(re1.period_stls, function(da, k) {
                                    if (da.period_no == 7) {
                                        $scope.disary.push(0);
                                    } else {
                                        $scope.disary.push(da.period_no);
                                    }
                                });
                            } else {
                                return dataService.periodstlGet({ query: { status: 0 } }).then(function(re2) {
                                    if (re2.period_stls.length > 0) {
                                        $scope.disary = [];
                                        _.map(re2.period_stls, function(da, k) {
                                            if (da.period_no == 7) {
                                                $scope.disary.push(0);
                                            } else {
                                                $scope.disary.push(da.period_no);
                                            }
                                        });
                                    }
                                })
                            }
                        })

                    }

                    // $scope.stltodat = function() {
                    //     var query = { "schema_id": $scope.schemaid };
                    //     dataService.stltodataAdd(query).then(function(re1) {
                    //         rmAlert();
                    //     });
                    // }
                }],
                link: function(scope, element, attrs, ueditor) {}
            }
        })
        .directive('periodstlone', ['dataService', function(dataService) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '=',
                },
                template: "<span>{{ngModel.name}}--({{ngModel.source_at}})</span>",
                link: function(scope, element, attrs, ctrl) {
                    //初始化，获取数据
                    ctrl.$render = function() {
                        if (_.isNumber(ctrl.$viewValue)) {
                            dataService.periodGet({
                                query: { period_id: ctrl.$viewValue }
                            }).then(function(re1) {
                                if (re1.periods) {
                                    ctrl.$setViewValue(re1.periods[0]);
                                }
                            })
                        }
                    };
                }
            }
        }])
        .directive('timeslotone', ['dataService', function(dataService) {
            return {
                restrict: 'E',
                require: 'ngModel',
                replace: true,
                scope: {
                    ngModel: '=',
                },
                template: "<span>{{ngModel.start}}-{{ngModel.end}}</span>",
                link: function(scope, element, attrs, ctrl) {
                    //初始化，获取数据
                    ctrl.$render = function() {
                        if (_.isNumber(ctrl.$viewValue)) {
                            dataService.timeslotGet({
                                query: { timeslot_id: ctrl.$viewValue }
                            }).then(function(re1) {
                                if (re1.timeslots) {
                                    ctrl.$setViewValue(re1.timeslots[0]);
                                }
                            })
                        }
                    };
                }
            }
        }]);
})();